package com.duke.shaking.widget;

import com.duke.shaking.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuffXfermode;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

public class RCImageView extends ImageView {
	
	private float cornerRatio = 5.0f;
	//private Path clipPath;
	//private RectF rectF;
	private Bitmap roundBitmap;
	private Bitmap fullSizeBitmap;
	
	public RCImageView(Context context) {  
        super(context);  
    }  

    public RCImageView(Context context, AttributeSet attrs) {  
        super(context, attrs);
        init(attrs);
    }  
      
    public RCImageView(Context context, AttributeSet attrs,  
            int defStyle) {  
        super(context, attrs, defStyle);
        init(attrs);
    }
    
    private void init(AttributeSet attrs) {
    	TypedArray a = getContext().obtainStyledAttributes(attrs,
                R.styleable.shaking);
		if (a == null) {
			return;
		}
		
		for (int i = 0; i < a.getIndexCount(); i ++) {
            int attr = a.getIndex(i);
            switch (attr) {
            case R.styleable.shaking_corner:
            	cornerRatio = a.getDimension(attr, cornerRatio);
            	break;
            }
		}
		a.recycle();
		
    }
    
    private Bitmap getRoundedCornerBitmap(Context context, Bitmap input, float pixels , int w , int h , boolean squareTL, boolean squareTR, boolean squareBL, boolean squareBR  ) {

        Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        final float densityMultiplier = context.getResources().getDisplayMetrics().density;

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, w, h);
        final RectF rectF = new RectF(rect);

        //make sure that our rounded corner is scaled appropriately
        final float roundPx = pixels * densityMultiplier;

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);


        //draw rectangles over the corners we want to be square
        if (squareTL ){
            canvas.drawRect(0, 0, w/2, h/2, paint);
        }
        if (squareTR ){
            canvas.drawRect(w/2, 0, w, h/2, paint);
        }
        if (squareBL ){
            canvas.drawRect(0, h/2, w/2, h, paint);
        }
        if (squareBR ){
            canvas.drawRect(w/2, h/2, w, h, paint);
        }

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(input, 0,0, paint);

        return output;
    }
    @Override  
    protected void onDraw(Canvas canvas) {
    	/*if (clipPath == null) {
    		clipPath = new Path();
    		int w = this.getWidth();  
            int h = this.getHeight();
        	if (rectF == null) {
        		rectF = new RectF(0, 0, w, h);
        	}
            
            clipPath.addRoundRect(rectF, 
            		cornerRatio, cornerRatio, Path.Direction.CW);  
    	}
        //canvas.clipPath(clipPath);
    	
        super.onDraw(canvas);  */
    	Drawable drawable = getDrawable();
    	if (drawable == null) {
    		super.onDraw(canvas);
    		return;
    	}
    	if (getWidth() == 0 || getHeight() == 0) {
    		super.onDraw(canvas);
            return; 
        }
    	
    	if (fullSizeBitmap != ((BitmapDrawable)drawable).getBitmap()) {
    		fullSizeBitmap = ((BitmapDrawable)drawable).getBitmap();
    		roundBitmap = null;
    		//Log.i("RCImageView, ", this + " redraw");
    	}
    	else {
    		//Log.i("RCImageView", this + " no need redraw");
    	}
    	
    	//fullSizeBitmap = ((BitmapDrawable)drawable).getBitmap();
        if (roundBitmap == null) {
        	int scaledWidth = getMeasuredWidth();
            int scaledHeight = getMeasuredHeight();
            Bitmap scaledBitmap;
            if (scaledWidth == fullSizeBitmap.getWidth() 
            	&& scaledHeight == fullSizeBitmap.getHeight()) {
	            scaledBitmap = fullSizeBitmap;
	        } else {
	            scaledBitmap = Bitmap.createScaledBitmap(fullSizeBitmap, scaledWidth, scaledHeight, true /* filter */);
	        }
        	roundBitmap = getRoundedCornerBitmap(getContext(), scaledBitmap, 
        			cornerRatio, scaledWidth, scaledHeight,
                false, false, false, false);
        }
 
        canvas.drawBitmap(roundBitmap, 0, 0, null);
    } 
}
